package com.yijinsuo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yijinsuo.R;
import com.yijinsuo.base.BaseActivity;
import com.yijinsuo.entity.TiXianHuifu;
import com.yijinsuo.http.HttpCallBack;
import com.yijinsuo.http.HttpUtils;
import com.yijinsuo.mian.Config;
import com.yijinsuo.response.ConfirmChongZhiRes;
import com.yijinsuo.tool.VibratorUtil;
import com.yijinsuo.util.HtmlFormUtil;
import com.yijinsuo.view.ClearEditText;
import com.yijinsuo.view.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 账户充值
 */
public class AccountChongZhiActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.tv_addMoney)
    TextView tvAddMoney;
    @Bind(R.id.et_add_money_number)
    ClearEditText etAddMoneyNumber;
    @Bind(R.id.tv_availableMoney)
    TextView tvAvailableMoney;
    //
    private String availableMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_account_chong_zhi);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {

        titlebar.setTitleBar(R.drawable.back_selector, "账户充值", 0);

        //
        availableMoney = getIntent().getStringExtra("availableMoney");
        tvAvailableMoney.setText(availableMoney);
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void setData() {
    }

    @OnClick({R.id.titlebar_iv_back, R.id.tv_addMoney})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.titlebar_iv_back:

                finish();

                break;
            case R.id.tv_addMoney:    //充值
                long[] pattern = {100, 400, 100, 400};
                String moneyNmuber = etAddMoneyNumber.getText().toString().trim();
                int number = 0;
                if (!TextUtils.isEmpty(moneyNmuber)) {
                    number = Integer.parseInt(moneyNmuber);
                }
                if (TextUtils.isEmpty(moneyNmuber)) {
                    etAddMoneyNumber.setShakeAnimation();
                    VibratorUtil.Vibrate(AccountChongZhiActivity.this, pattern, false);
                    Toast.makeText(AccountChongZhiActivity.this, "充值金额不能为空", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    sendtoRecharge(number);

                }

                break;

            default:
        }
    }

    /**
     * 账户充值
     */

    private void sendtoRecharge(int number) {

        HttpParams params = new HttpParams();
        params.put("payMoney",number+"");

        HttpUtils.post(Config.RECHARGE, params, new HttpCallBack(this, "加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if(null!=result){
                    try {
                        ConfirmChongZhiRes res=new ConfirmChongZhiRes(new JSONObject(result.toString()));
                        TiXianHuifu info=null;
                        if(null!=res.getHuifu()){
                            info=res.getHuifu();
                            Map<String ,String> params=new HashMap<String, String>();
                            params.put("login_id",info.getLoginId());
                            params.put("mchnt_cd",info.getMchntCd());
                            params.put("back_notify_url",info.getBackNotifyUrl());
                            params.put("back_notify_url",info.getBackNotifyUrl());
                            params.put("amt",info.getAmt());
                            params.put("page_notify_url",info.getPageNotifyUrl());
                            params.put("mchnt_cd",info.getMchntCd());
                            try {
                                params.put("signature", URLEncoder.encode(info.getSignature(),"UTF-8"));
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            params.put("mchnt_txn_ssn",info.getMchntTxnSsn());
                            Intent intent=new Intent(AccountChongZhiActivity.this,WebActivity.class);
                            intent.putExtra("url",info.getGateWayUrl());
                            intent.putExtra("postData", HtmlFormUtil.makePostHTML(info.getGateWayUrl(),params));
                            intent.putExtra("titile_text","确认充值");
                            try {
                                intent.putExtra("params", HtmlFormUtil.paramMap2bytes(params));
                                startActivity(intent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }


            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(AccountChongZhiActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });

    }
}
